/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.vdab.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 *
 * @author sven.vanaerschot
 */
public class HibernateUtil {
    private static final SessionFactory sessionFactory;
    static{
        try{
            Configuration configuration = new Configuration();
            configuration.configure();
            ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder();
            serviceRegistryBuilder.applySettings(configuration.getProperties());
            ServiceRegistry serviceRegistry = serviceRegistryBuilder.buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch(Throwable ex){
            System.err.println("Initial SessionFactory creation faled." + ex);
            ex.printStackTrace(System.err);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
    
}
